package ch.unisi.inf.pfii.teamred.pacman.view;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;

import javax.swing.JLabel;
import javax.swing.JPanel;

import ch.unisi.inf.pfii.teamred.pacman.controller.PlayerListener;
import ch.unisi.inf.pfii.teamred.pacman.model.Player;

/**
 * This Class models the view of the Player of the Pacman Game.
 * 
 * @author mark.pruneri@lu.unisi.ch
 * 
 */

public class PlayerView extends JPanel {

	private final Player player;

	private final JLabel scoreLabel;
	private final JLabel livesLabel;

	public PlayerView(final Player player) {
		this.player = player;

		player.addPlayerListener(new PlayerListener() {
			public final void playerSetScore(final Player player) {
				scoreLabel.setText("" + player.getScore());
				repaint();
			}

			public final void playerSetLife(final Player player) {
				livesLabel.setText("" + player.getLife());
				repaint();
			}

			public final void playerUpdatedScore(final Player player) {
				scoreLabel.setText("" + player.getScore());
				repaint();
			}

			public void playerIsDead(Player player) {
				repaint();
			}
		});

		scoreLabel = new JLabel("" + player.getScore());
		livesLabel = new JLabel("" + player.getLife());

		setLayout(new FlowLayout(FlowLayout.CENTER));

		add(new JLabel("SCORE: "));
		add(scoreLabel);
		add(new JLabel(" - "));
		add(new JLabel("LIVES: "));
		add(livesLabel);

		setVisible(true);

	}

	public final void paintComponent(Graphics g) {
		super.paintComponent(g);

		g.setColor(Color.WHITE);
		g.fillRect(0, 0, getWidth(), getHeight());
	}
}
